﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="html" indent="yes" omit-xml-declaration="yes" />
  <xsl:param name="MachineName">oMachine</xsl:param>
  <xsl:variable name="LineFeed"><xsl:text>&#10;&#13;</xsl:text></xsl:variable>
  
  <xsl:template match="GIVisualMachine">
    var <xsl:value-of select="$MachineName"/> = new GIMachine("<xsl:value-of select="$MachineName"/>");
    <!-- First we create all States -->
    <xsl:for-each select="Nodes/Node">
      <xsl:variable name="JSFriendlyPattern">
        <xsl:call-template name="string-replace-all">
          <xsl:with-param name="text" select="Pattern" />
          <xsl:with-param name="replace">\</xsl:with-param>
          <xsl:with-param name="by" >\\</xsl:with-param>
        </xsl:call-template>
      </xsl:variable>
      var <xsl:value-of select="$MachineName"/>_S<xsl:value-of select="Id" disable-output-escaping="yes"/> = new GIMachineWordState("<xsl:value-of select="$JSFriendlyPattern" disable-output-escaping="yes" />", <xsl:value-of select="IsLiteral" disable-output-escaping="yes" />,<xsl:value-of select="Id" disable-output-escaping="yes" />,"<xsl:value-of select="Name" disable-output-escaping="yes" />", <xsl:value-of select="IsAcceptable" disable-output-escaping="yes" />);
    </xsl:for-each>
    
    
    <!-- Now establish the relationship / edge -->
    <xsl:for-each select="Edges/Edge">
      <xsl:sort select="FromNode" />
      <xsl:variable name="StateVariableName"><xsl:value-of select="$MachineName"/>_S<xsl:value-of select="FromNode" disable-output-escaping="yes"/></xsl:variable>
      <xsl:value-of select="$StateVariableName"/>.NextStates[<xsl:value-of select="$StateVariableName"/>.NextStates.length] = <xsl:value-of select="$MachineName"/>_S<xsl:value-of select="ToNode" disable-output-escaping="yes"/>;
    </xsl:for-each>
    
    <xsl:for-each select="Nodes/Node[ IsStartState = 'true' ]">
      <xsl:variable name="stateIndex" select="position()-1"/>
      <xsl:value-of select="$MachineName"/>.States[<xsl:value-of select="$MachineName"/>.States.length] = <xsl:value-of select="$MachineName"/>_S<xsl:value-of select="Id" disable-output-escaping="yes"/>;
    </xsl:for-each>
    
  </xsl:template>

  <xsl:template name="string-replace-all">
    <xsl:param name="text" />
    <xsl:param name="replace" />
    <xsl:param name="by" />
    <xsl:choose>
      <xsl:when test="contains($text, $replace)">
        <xsl:value-of select="substring-before($text,$replace)" />
        <xsl:value-of select="$by" />
        <xsl:call-template name="string-replace-all">
          <xsl:with-param name="text"
          select="substring-after($text,$replace)" />
          <xsl:with-param name="replace" select="$replace" />
          <xsl:with-param name="by" select="$by" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


</xsl:stylesheet>
